/*
 ------------------------------------------------------------------------------
 Copyright (C) 2006-2007 Team Blur.

 This file is part of the Quantum Engine source code.

 The Quantum Engine source code is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or (at your
 option) any later version.

 The Quantum Engine source code is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 more details.

 You should have received a copy of the GNU General Public License along with
 the Quantum Engine source code; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 ------------------------------------------------------------------------------
*/


#include "cg_local.h"


/*
 ==================
 CG_AddEntities
 ==================
*/
void CG_AddEntities (void){

	cgEntity_t		*entity;
	entityState_t	*state;
	int				i;

	for (i = 0; i < cgs.snapshot.numEntities; i++){
		state = &cgs.parseEntities[(cgs.snapshot.parseEntitiesNum + i) & (MAX_PARSE_ENTITIES - 1)];

		entity = &cg.entities[state->number];

		// TODO: only update render lights if the entity changed in some way
		// (which can be detected during the delta parse)
		if (state->type == ET_LIGHT){
			renderLight_t	renderLight;

			if (state->projected)
				renderLight.type = RL_PROJECTED;
			else
				renderLight.type = RL_POINT;

			renderLight.lightNum = state->number;

			renderLight.origin = state->origin;
			renderLight.center = state->center;
			renderLight.axis = state->angles.ToMat3();
			renderLight.radius = state->radius;
			renderLight.fovX = state->fovX;
			renderLight.fovY = state->fovY;
			renderLight.falloffRange = state->falloffRange;
			renderLight.parallel = state->parallel;
			renderLight.noShadows = state->noShadows;
			renderLight.detailLevel = state->detailLevel;
			renderLight.allowInView = VIEW_ANY;
			renderLight.material = cgs.gameMaterials[state->materialIndex];
			memcpy(renderLight.materialParms, state->materialParms, MAX_MATERIAL_PARMS * sizeof(float));

			if (entity->lightHandle == -1)
				entity->lightHandle = odRenderer->AddRenderLight(cgs.scene, &renderLight);
			else
				odRenderer->UpdateRenderLight(cgs.scene, entity->lightHandle, &renderLight);
		}
	}
}
